//
//  WorkFlowViewController.m
//  nncloud
//
//  Created by macfeel on 2019/6/14.
//  Copyright © 2019 macfai. All rights reserved.
//
#import "ZFScanViewController.h"
#import "WorkFlowViewController.h"
#import "EquipTypeSwitchViewController.h"
#import "HandoutAndSignViewController.h"
#import "NBusinessServiceViewController.h"
#import "NServeCell.h"
#import "NMyFeedbackViewController.h"
#import "NCommitFBViewController.h"
#import "NWriteMsgViewController.h"

@interface WorkFlowViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) NSArray *titleArray;
@property (nonatomic,strong) NSArray *titleArray1;
@property (nonatomic,strong) NSArray *titleArray2;
@property (nonatomic,strong) NSArray *titleArray3;
@property (nonatomic,strong) NSArray *titleArray4;

@property (nonatomic,strong) NSArray *iconArray;
@property (nonatomic,strong) NSArray *iconArray1;
@property (nonatomic,strong) NSArray *iconArray2;
@property (nonatomic,strong) NSArray *iconArray3;
@property (nonatomic,strong) NSArray *iconArray4;

@property (nonatomic,strong) NSArray *sectionTitleArray;

@property (nonatomic,assign)BOOL isScan;

@property (nonatomic,copy)NSString *scanResult;

@end

#define kLineNumber 4
#define NavHeight 64
#define SectionHeaderHeight 65
NSString *const cellID = @"serveid";
NSString *const headReuseID = @"headReuse";

@implementation WorkFlowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = nil;
    [self.navigationItem setItemWithTitle:@"工作流" textColor:kNavigationTitleColor fontSize:18 itemType:center];
     [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 30, 30) title:@"" Image:@"nav_scan" selectedIMG:nil tartget:self action:@selector(scan)];
    
    [self data];
    [self.view addSubview:self.collectionView];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = false;
}

-(void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = YES;
    
}


///组装数据
- (void)data {
    
  
    self.sectionTitleArray = @[@"工作流",@"现场作业",@"材料管理",];
    
    self.titleArray = @[@"采集核对",@"发放签收",@"备品管理",@"故障管理",@"计划执行",];
    
    self.iconArray = @[@"问题3",@"未标题-2",@"未标题-3",@"未标题-4",@"未标题-9",];
    
    self.titleArray1 = @[@"现场作业",@"站场信息",];
    self.iconArray1 = @[@"未标题-5",@"未标题-6",];
//
//
    self.titleArray2 = @[@"材料管理",@"材料信息",];
    self.iconArray2 = @[@"未标题-8",@"未标题-7",];
//
//
//    self.titleArray3 = @[@"智慧招商",@"创新创业项目",@"知识产权服务",@"项目申报",@"培训服务",@"融资服务",@"项目路演"];
//    self.iconArray3 = @[@"智慧招商",@"创新创业项目",@"知识产权服务",@"项目申报",@"培训服务",@"科技金融",@"项目路演"];
//
//
//
//        self.titleArray4 = @[@"企业办事",@"企业招聘",@"求职信息",@"政府在线",@"职称评审"];
//        self.iconArray4 = @[@"企业办事",@"企业招聘",@"求职信息",@"政府在线",@"职称评审"];

}

//流式布局
- (UICollectionViewFlowLayout *)flowLayout {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(15, 0, 5, 0);
    layout.minimumLineSpacing = 0.f;
    layout.minimumInteritemSpacing = 0.f;
    //    layout.headerReferenceSize = CGSizeMake(kScreenWidth, SectionHeaderHeight);
    return layout;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-NavHeight-50) collectionViewLayout:[self flowLayout]];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        [self registerCell];
        //        WeakStament(ws);
        //        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //            [ws refreshData];
        //        }];
        _collectionView.backgroundColor = kWhiteColor;
    }
    return _collectionView;
}

- (void)registerCell {
    
    [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([NServeCell class]) bundle:nil] forCellWithReuseIdentifier:cellID];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headReuseID];
    
}

//点击扫码
- (void)scan {
    
    ///扫码
    ///present 弹出慢解决
//     [self loadWithNumber:@"10126"];
    
    
    
    
    ///模拟扫码数据测试
//    EquipTypeSwitchViewController *type = [EquipTypeSwitchViewController new];
//    [NUserModel shareInstance].scanEquipType = @(3);
//    [NUserModel shareInstance].scanBrokenNumber = @"36808000015";;
//    [self.navigationController pushViewController:type animated:YES];
//    return;
    
    
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        ZFScanViewController * vc = [[ZFScanViewController alloc] init];
        
        vc.returnScanBarCodeValue = ^(NSString * barCodeString){
            
//            [AFToast showText:barCodeString];
            //        self.resultLabel.text = [NSString stringWithFormat:@"扫描结果:\n%@",barCodeString];
            if (barCodeString.length>3) {
                
                self.scanResult = barCodeString;
                ///根据这个码请求数据后获取 对应的器材类型  type
                [NUserModel shareInstance].scanBrokenNumber = barCodeString;
                /// 根据请求到的类型进行跳转  需要进行跳转 根据类型进入到
                [self loadWithNumber:barCodeString];
               
                ///如果放在外层就会有问题，如果仅仅是打开了一下扫描，但是没有扫，如果进到采集核对页面会发现请求数据时异常
                [NUserModel shareInstance].comeinWithScan = YES;
                
            }else{
                [AFToast showText:@"当前扫描结果无效"];
            }
        };
        
        [self presentViewController:vc animated:YES completion:nil];
        
    });
}



#pragma mark - 根据段编号进行请求 获取当前对应的器材类型以便在内层tabbar进行索引选择跳转
-(void)loadWithNumber:(NSString *)number{
    
    NSDictionary *dict = @{@"brokenNumber":number};
    [NNetworkClient postRequestWithBaseUrl:kScanUrl setParameters:dict success:^(id responseObject) {
        YYLog(@"%@",responseObject);
        if ([responseObject[@"status"]integerValue]==0) {
            
            ///根据这个类型决定进入后跳到哪个 tabbar 索引
            [NUserModel shareInstance].scanEquipType = responseObject[@"type"];
            
            [self goToEquipList];
            
        }else{
            
//            [AFToast showText:responseObject[@"message"]];
            ///failure分之
            [AFToast showText:@"当前扫描未找到结果"];
            
        }
    } fail:^(NSError *error) {
        
    } delegater:nil];
    
}

-(void)goToEquipList{
    
    ///内层tabbar控制器
    EquipTypeSwitchViewController *equipTab = [EquipTypeSwitchViewController new];
    [self.navigationController pushViewController:equipTab animated:YES];
    
}

#pragma  mark - datasource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

        return self.sectionTitleArray.count;
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
        switch (section) {
            case 0:
            {
                return self.titleArray.count;
            }
                break;
            case 1:
            {
                return self.titleArray1.count;
            }
                break;
            case 2:
            {
                return self.titleArray2.count;
            }
                break;
//            case 3:{
//                return 7;
//            }
//                break;
//            case 4:
//            {
//                return 5;
//            }
//                break;
                
            default:
                return 0;
                break;
        }
}
///cell
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
        //企业端
        NServeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
        if (indexPath.section==0) {
            
            cell.nameLabel.text = self.titleArray[indexPath.row];
            cell.iconImage.image = WDImgName(self.iconArray[indexPath.row]);
            
        }
        else if(indexPath.section==1){

            cell.nameLabel.text = self.titleArray1[indexPath.row];
            cell.iconImage.image = WDImgName(self.iconArray1[indexPath.row]);

        }else if (indexPath.section==2){

            cell.nameLabel.text = self.titleArray2[indexPath.row];
            cell.iconImage.image = WDImgName(self.iconArray2[indexPath.row]);

        }
//        else if (indexPath.section==3){
//            cell.nameLabel.text = self.titleArray3[indexPath.row];
//            cell.iconImage.image = WDImgName(self.iconArray3[indexPath.row]);
//        }else{
//            cell.nameLabel.text = self.titleArray4[indexPath.row];
//            cell.iconImage.image = WDImgName(self.iconArray4[indexPath.row]);
//        }
        return cell;
        

}


-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusabelView = nil;
    //    XALifeListModel *model = self.dataArray[indexPath.section-BannerIndex];
    if (kind == UICollectionElementKindSectionHeader) {
        
        ///这个是中间分割的那个区域
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, SectionHeaderHeight)];
        bgView.backgroundColor = [UIColor clearColor];
        UIView *sepratorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 11)];
        sepratorView.backgroundColor = NColorFrom16RGB(0xeeeeee);
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, ScreenWidth, SectionHeaderHeight-5)];
        //        titleLabel.text = model.title;
        
        ////注意，如果是多个区的话，这里要放开
        titleLabel.text = self.sectionTitleArray[indexPath.section];
        
//        titleLabel.text = @"工作流";
        
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headReuseID forIndexPath:indexPath];
        [headerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        
        [headerView addSubview:bgView];
//        [headerView addSubview:sepratorView];
        [headerView addSubview:titleLabel];
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, SectionHeaderHeight-6, ScreenWidth, 1)];
        line.backgroundColor = NColorFrom16RGB(0xeeeeee);
//        [headerView addSubview:line];
        reusabelView = headerView;
    }
    return reusabelView;
}


#pragma mark - delegate
///section header 尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(ScreenWidth, SectionHeaderHeight-20);
}

//cell尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat width=ScreenWidth/kLineNumber;
    return CGSizeMake(width, width);
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section==0) {
        switch (indexPath.row) {
            case 0:
            {
                EquipTypeSwitchViewController *hh = [EquipTypeSwitchViewController new];
                
                //这个保证如果是从采集核对进去的显示的是第一个 ，相当于是没扫
                [NUserModel shareInstance].scanEquipType = @(1);
                
                [self.navigationController pushViewController:hh animated:YES];
            }
                break;
                
            case 1:{
                HandoutAndSignViewController *hand = [HandoutAndSignViewController new];
                hand.name = @"发放签收";
                [self.navigationController pushViewController:hand animated:YES];
            }
                break;
            case 2:{
                HandoutAndSignViewController *hand = [HandoutAndSignViewController new];
                hand.name = @"备品管理";
                [self.navigationController pushViewController:hand animated:YES];
            }
                break;
                
            case 3:{
                HandoutAndSignViewController *hand = [HandoutAndSignViewController new];
                hand.name = @"故障管理";
                [self.navigationController pushViewController:hand animated:YES];
            }
                break;
            case 4:{
                HandoutAndSignViewController *hand = [HandoutAndSignViewController new];
                hand.name = @"计划执行";
                [self.navigationController pushViewController:hand animated:YES];
            }
                break;
//            case 5:{
//                HandoutAndSignViewController *hand = [HandoutAndSignViewController new];
//                hand.name = @"材料管理";
//                [self.navigationController pushViewController:hand animated:YES];
//            }
//                break;
//
            default:
            {
                //                [AFToast showText:@"开发中……"];
                
            }
                break;
        }
    }else if (indexPath.section==1){
        HandoutAndSignViewController *hand = [HandoutAndSignViewController new];
        hand.name = @"现场";
        [self.navigationController pushViewController:hand animated:YES];
    }else{
        HandoutAndSignViewController *hand = [HandoutAndSignViewController new];
        hand.name = @"材料";
        [self.navigationController pushViewController:hand animated:YES];
    }
}


@end
